//
//  CommentModel.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "CommentModel.h"

@implementation CommentModel
- (id)initContentWithDic:(NSDictionary *)jsonDic {
    self = [super init];
    if (self != nil) {
    [self setAttributes:jsonDic];
        _content = jsonDic[@"message"];
        if ([jsonDic[@"comment_num"] integerValue] == 0) {
            _comment = NO;
        }else{
            _comment = YES;
        }
        
        //头像
        NSDictionary *userInfoDic= jsonDic[@"user_info"];
        NSString *head = userInfoDic[@"head"];
//        _authorImage = [NSString stringWithFormat:@"%@%@",BOYE_PICTURE_URL,head];
        if ([head checkPositiveInteger]) {
            _authorImage = [NSString stringWithFormat:@"%@%@",BOYE_PICTURE_URL,head];
        }else{
            _authorImage = head;
        }

        _nickname = userInfoDic[@"nickname"];
        if (_nickname.length == 0) {
            _nickname = _author;
        }
        id grDic = userInfoDic[@"group"];
        if (![grDic isKindOfClass:[NSNull class]]) {
            _isAllowpost = grDic[@"allowpost"];
        }
        if (_isAllowpost == nil) {
            _isAllowpost = @"1";
        }

        //回复
        if (![jsonDic[@"comments"] isKindOfClass:[NSNull class]]) {
        NSArray *reArray = jsonDic[@"comments"];
        if (reArray.count == 1) {
            NSDictionary *reDic1 = reArray[0];
            NSDictionary *userInfo1 = reDic1[@"user_info"];
            if (![userInfo1[@"nickname"] isKindOfClass:[NSNull class]]) {
                NSString *nick1 = userInfo1[@"nickname"];
                if (nick1.length == 0) {
//                    nick1 = userInfo1[@"username"];
                    nick1 = reDic1[@"author"];
                }
                _reply1 = [NSString stringWithFormat:@"%@ 回复: %@",nick1,reDic1[@"comment"]];
            }else{
                NSString *nick1 = reDic1[@"author"];
                _reply1 = [NSString stringWithFormat:@"%@ 回复: %@",nick1,reDic1[@"comment"]];
            }
           
            
            
        }else{
            NSDictionary *reDic1 = reArray[0];
            NSDictionary *userInfo1 = reDic1[@"user_info"];
            NSDictionary *reDic2 = reArray[1];
            NSDictionary *userInfo2 = reDic2[@"user_info"];
            if (![userInfo1[@"nickname"] isKindOfClass:[NSNull class]]) {
                NSString *nick1 = userInfo1[@"nickname"];
                if (nick1.length == 0) {
                    nick1 = userInfo1[@"username"];
                }
                _reply1 = [NSString stringWithFormat:@"%@ 回复: %@",nick1,reDic1[@"comment"]];
            }else{
                NSString *nick1 = userInfo1[@"username"];
                _reply1 = [NSString stringWithFormat:@"%@ 回复: %@",nick1,reDic1[@"comment"]];
            }
            if (![userInfo2[@"nickname"] isKindOfClass:[NSNull class]]) {
                NSString *nick2 = userInfo2[@"nickname"];
                if (nick2.length == 0) {
                    nick2 = userInfo2[@"username"];
                }
                _reply2 = [NSString stringWithFormat:@"%@ 回复: %@",nick2,reDic2[@"comment"]];
            }else{
                NSString *nick2 = userInfo2[@"username"];
                _reply2 = [NSString stringWithFormat:@"%@ 回复: %@",nick2,reDic2[@"comment"]];
            }
        }
        }
        //图片数组
        _imgAry = [[NSMutableArray alloc] init];
        NSArray *tmpArray;
        tmpArray = jsonDic[@"img"];
        if (![jsonDic[@"img"] isKindOfClass:[NSNull class]]){
            for (NSString *url in tmpArray) {
                NSString *str = [NSString stringWithFormat:@"%@%@",BOYE_PICTURE_URL,url];
                [_imgAry addObject:str];
            }
        }
    }
    return self;
}
@end
